package com.levy.jituan;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

public class KeyUtils {

    private static String[] keyStr = {
//            "6ed2fbf88739f91de9290099407d348b",
            "fb81b1d0fe64e4090785fe4a77ecfc6b",
            "4c804999ddb77905f9f1501fad0f62f1",
            "5e3f155c601edf50077b570f72b9fa3a",
//            "d39d7c6e358a5ab68859f9b7050c4712"
//            "3bacb7d738269ad3d827783197f44df1",
//            "5e3f155c601edf50077b570f72b9fa3a",
//            "260916349e6753f6ec0917b0a7bfaacc",
//            "8201249e12c0877afe0d59423153ba3a",
//            "a2955f65bbee2ce3f6d76bc294cff0c8",
//            "a34ac372ea7104509131105c398ab7b2",
//            "d39d7c6e358a5ab68859f9b7050c4712",
//            "0cc924da06692ae0f3c3954bda749054",
//            "eef73804d6c157799cac6a33cac229ab",
//            "5e7d7baeb4a5bd4999fead695c3813dc",
//            "2885b334aa69722d21843176e9c9be34",
//            "fc6c084e5a9f4a3832e57e36c99cdbf3",
//            "1dd89b338fd028caa796d4a569fcabfe",
//            "ac93b83edc6e0e327e10a301f53608bb",
//            "fbe8726b11a28a0715f0b09960a31931",
//            "4fbdf8aace93a6cf2f4cdf135049e15a",
//            "f215b2c33ac45389ed2359892bc0f646",
//            "d2c5ad48cfdff984e3c92a7ba4e629e3",
//            "65dacb6f3d78d74f42be38f3a1f6b5df",
//            "69a73f60d3ee3ebce96cf233ef08b409",
//            "d39d7c6e358a5ab68859f9b7050c4712"
    };

    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    private static String errKeyFile = "G:\\经纬度\\zulin\\err_key.txt";

    private static int size = 0;

    private static Vector<String> avaliabelKeyVector = new Vector<String>();

    static {

        boolean exist = FileUtil.exist(errKeyFile);

        List<String> errKeyList = null;
        if (exist) {
            errKeyList = FileUtil.readUtf8Lines(errKeyFile);
            if (errKeyList != null && errKeyList.size() > 0) {
                for (String s : keyStr) {
                    if (!errKeyList.contains(s)) {
                        avaliabelKeyVector.add(s);
                    }
                }
            }
        } else {
            Arrays.stream(keyStr).forEach(o -> {
                avaliabelKeyVector.add(o);
            });
        }

        size = avaliabelKeyVector.size();
    }

    public static String getRandomKey() {
        synchronized (lock1) {
            if (avaliabelKeyVector.size() > 0) {
                int i = new Random().nextInt(avaliabelKeyVector.size());
                return avaliabelKeyVector.get(i);
            }
            return null;
        }
    }

    public static void removeKey(String key) {
        synchronized (lock2) {
            if (key == null) {
                return;
            }
            avaliabelKeyVector.remove(key);
            if (FileUtil.exist(errKeyFile)) {
                List<String> strings = FileUtil.readUtf8Lines(errKeyFile);
                if (!strings.contains(key)) {
                    FileUtil.appendUtf8String(key + "\r\n", errKeyFile);
                }
            }
        }
    }

    public static boolean jugKeyIsAvaliabel(String key) {
        String getLocationUrl = "https://restapi.amap.com/v3/geocode/geo?key=$Key&city=广州&address=广州塔";
        System.out.println(getLocationUrl.replaceAll("\\$Key", key));
        String res = HttpUtil.get(getLocationUrl.replaceAll("\\$Key", key));
        System.out.println(res);
        JSONObject jsonObject = JSON.parseObject(res);
        if ("1".equals(jsonObject.get("status"))) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        for (String s : avaliabelKeyVector) {
            jugKeyIsAvaliabel(s);
        }

    }


}
